﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
using System.Threading;
using System.Windows.Threading;
using MOJsdk;

using IO = System.IO;
using System.Windows.Media.Imaging;

namespace AndroidRemote
{
    public class ServerActions
    {
        public enum Actions
        {
            Test = 0,

            WFocus = 10,

            ShowMessage = 20,

            GetSelectedMovie = 30,
            SelectNextMovie = 31,
            SelectPrevMovie = 32,
            GetMovieInfo = 33,
            GetMovieCover = 34,
            getSelectedmovieCover = 35,
            getSelectedMovieInfo = 36,

            Mouse = 99,
            MouseLeft = 100,
            MouseRight = 101,
        }
        public ServerActions()
        {

        }

        public static byte[] ExecuteAction(byte[] Action, byte[] Args)
        {
            IHost _host = MojHost.GetHost;
            Actions action = (Actions)(Encoding.ASCII.GetString(Action).ToInt());

            switch (action)
            {
                #region INIActions
                case Actions.Test:
                    byte[] data = Encoding.ASCII.GetBytes(true.ToString());
                    return data;
                #endregion

                #region WindowsActions
                case Actions.WFocus:
                    _host.ActionFocus();
                    return Encoding.ASCII.GetBytes("done");

                case Actions.ShowMessage:
                    return showMessage(Args);
                #endregion

                #region Mouse&Keys
                case Actions.Mouse:
                    return exeMouseMove(Args);

                case Actions.MouseLeft:
                    TOOLS.LeftMouseClick();
                    return Encoding.ASCII.GetBytes("done");

                case Actions.MouseRight:
                    TOOLS.RightMouseClick();
                    return Encoding.ASCII.GetBytes("done");
                #endregion

                #region Movies
                case Actions.SelectNextMovie:
                    _host.SelectNextMovie();
                    if(MojStatusVarsEnum.IsMovieSelected.Value())
                        return Encoding.ASCII.GetBytes(MojStatusVarsEnum.SelectedMovie.Value());
                    break;

                case Actions.SelectPrevMovie:
                    _host.SelectPrevMovie();
                    if (MojStatusVarsEnum.IsMovieSelected.Value())
                        return Encoding.ASCII.GetBytes(MojStatusVarsEnum.SelectedMovie.Value());
                    break;

                case Actions.GetSelectedMovie:
                    if (MojStatusVarsEnum.IsMovieSelected.Value())
                        return Encoding.ASCII.GetBytes(MojStatusVarsEnum.SelectedMovie.Value());
                    else
                    {
                        _host.SelectNextMovie();
                        if (MojStatusVarsEnum.IsMovieSelected.Value())
                            return Encoding.ASCII.GetBytes(MojStatusVarsEnum.SelectedMovie.Value());
                        break;
                    }

                case Actions.GetMovieCover:
                    return getMovieCover(Args);

                case Actions.getSelectedmovieCover:
                    return getSelectMovieCover(Args);

                case Actions.GetMovieInfo:
                    return getMovieDetails(Args);


                #endregion
            }

            return Encoding.ASCII.GetBytes("null");
        }

        private static byte[] exeMouseMove(byte[] Args)
        {
            IHost _host = MojHost.GetHost;
            string data = Encoding.ASCII.GetString(Args);
            List<string> pre = new List<string>(data.ToList(";"));

            int xMov = pre[0].ToInt();
            int yMov = pre[1].ToInt();

            _host.TheMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                (ThreadStart)delegate
            {
                Point mPos = TOOLS.GetCursorPos();

                mPos.X += xMov;
                mPos.Y += yMov;

                TOOLS.SetMousePos(mPos);
            });

            return Encoding.ASCII.GetBytes("null");
        }
        private static byte[] showMessage(byte[] Args)
        {
            string data = Encoding.ASCII.GetString(Args);
            List<string> list = new List<string>(data.ToList(";"));

            MojHost.GetHost.ShowMessage(list[1], list[0]);

            return Encoding.ASCII.GetBytes("done");
        }

        private static byte[] getSelectMovieCover(byte[] Args)
        {
            return new byte[1];
        }
        private static byte[] getMovieCover(byte[] Args)
        {
            return new byte[1];
        }
        private static byte[] getMovieDetails(byte[] Args)
        {
            string code = Encoding.ASCII.GetString(Args);
            MojMovie m = new MojMovie(code);
            return new byte[1];
        }
    }
}
